﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Infrastructure.MenuService
{
    public class MenuConfiguration : ConfigurationSection
    {
        [ConfigurationProperty("MenuItems", IsDefaultCollection = true)]
        public MenuItemCollection MenuItems
        {
            get
            {
                return (MenuItemCollection)(this["MenuItems"]);
            }
        }
    }

    [ConfigurationCollection(typeof(MenuItem), AddItemName = "MenuItem", RemoveItemName = "removeMenuItem")]
    public class MenuItemCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new MenuItem();
        }
        public void AddMenuItem(ConfigurationElement element)
        {
            base.BaseAdd(element);
        }
        public void AddMenuItems(ConfigurationElementCollection elements)
        {
            foreach (var element in elements)
            {
                if (element is ConfigurationElement)
                {
                    base.BaseAdd((ConfigurationElement)element);
                }
            }
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            MenuItem e = (MenuItem)element;

            return e.ID;
        }
        public new MenuItem this[string id]
        {
            get { return (MenuItem)BaseGet(id); }
        }

        public MenuItem this[int index]
        {
            get { return (MenuItem)BaseGet(index); }
        }
    }

    public class MenuItem : ConfigurationElement
    {
        [ConfigurationProperty("id", IsRequired = true, IsKey = true)]
        public string ID
        {
            get { return (string)this["id"]; }
            set { this["id"] = value; }
        }

        [ConfigurationProperty("text", IsRequired = true)]
        public string Text
        {
            get { return (string)this["text"]; }
            set { this["text"] = value; }
        }

        [ConfigurationProperty("form", IsRequired = false)]
        public string Form
        {
            get { return (string)this["form"]; }
            set { this["form"] = value; }
        }

        [ConfigurationProperty("icon", IsRequired = false)]
        public string Icon
        {
            get { return (string)this["icon"]; }
            set { this["icon"] = value; }
        }
        [ConfigurationProperty("MenuItems")]
        public MenuItemCollection MenuItems
        {
            get
            {
                return (MenuItemCollection)base["MenuItems"];
            }
        }
    }
}
